package com.das.mechanic_base.gen;

import com.das.mechanic_base.bean.live.LiveSelectCarBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.ArrayList;
import java.util.List;

public class CarMemberBeanConVerter implements PropertyConverter<List<LiveSelectCarBean>, String> {
        @Override
        public List<LiveSelectCarBean> convertToEntityProperty(String databaseValue) {
            if (databaseValue == null) {
                return null;
            } else {
                JsonArray jsonArray = new JsonParser().parse(databaseValue).getAsJsonArray();
                Gson gson = new Gson();
                List<LiveSelectCarBean> recordKeyBeans = new ArrayList<>();
                //循环遍历
                for (JsonElement user : jsonArray) {
                    LiveSelectCarBean keyBean = gson.fromJson(user, LiveSelectCarBean.class);
                    recordKeyBeans.add(keyBean);
                }
                return recordKeyBeans;
            }
        }

        @Override
        public String convertToDatabaseValue(List<LiveSelectCarBean> entityProperty) {
            if (entityProperty == null) {
                return null;
            } else {
                return new Gson().toJson(entityProperty);
            }
        }
}
